﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;
using System.Net;
using System.Threading;

namespace gTray
{
    public class FeedParser
    {
        /// <summary>
        /// Parses the gmail feed.
        /// </summary>
        /// <param name="feed">The feed.</param>
        public static void ParseGmailFeed(ref Feed feed, NetworkCredential credentials)
        {
            XmlReader reader;
            XmlUrlResolver resolver;
            XmlReaderSettings settings;
            XmlDocument doc;
            List<FeedEntry> newEntries;
            DateTime temp = new DateTime();

            if (feed == null)
                feed = new Feed();

            resolver = new XmlUrlResolver();
            resolver.Credentials = credentials;

            settings = new XmlReaderSettings();
            settings.XmlResolver = resolver;

            try
            {
                reader = XmlReader.Create(Utilities.Gmail_Feed_Address, settings);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            doc = new XmlDocument();
            doc.Load(reader);

            XmlNode feedNode = null;
            XmlNode entryNode;

            for (int i = 0; i < doc.ChildNodes.Count; i++)
            {
                if (doc.ChildNodes[i].Name == "feed")
                    feedNode = doc.ChildNodes[i];
            }

            feed.Title = feedNode["title"].InnerText;
            feed.Count = int.Parse(feedNode["fullcount"].InnerText);
            feed.Link = new Uri(feedNode["link"].Attributes["href"].Value);


            if (DateTime.TryParse(feedNode["modified"].InnerText, out temp))
                feed.Modified = temp;

            newEntries = new List<FeedEntry>();
            for (int i = 0; i < feedNode.ChildNodes.Count; i++)
            {
                if (feedNode.ChildNodes[i].Name == "entry")
                {
                    entryNode = feedNode.ChildNodes[i];

                    FeedEntry entry = new FeedEntry();

                    entry.Title = entryNode["title"].InnerText;
                    entry.Summary = entryNode["summary"].InnerText;
                    entry.Link = new Uri(entryNode["link"].Attributes["href"].Value);

                    temp = new DateTime();
                    if (DateTime.TryParse(entryNode["modified"].InnerText, out temp))
                        entry.Modified = temp;

                    temp = new DateTime();
                    if (DateTime.TryParse(entryNode["issued"].InnerText, out temp))
                        entry.Issued = temp;

                    entry.ID = entryNode["id"].InnerText;

                    for (int j = 0; j < entryNode.ChildNodes.Count; j++)
                    {
                        if (entryNode.ChildNodes[j].Name == "author")
                        {
                            entry.AuthorName = entryNode.ChildNodes[j]["name"].InnerText;
                            entry.AuthorEmail = entryNode.ChildNodes[j]["email"].InnerText;
                        }

                    }
                    newEntries.Add(entry);
                }
            }

            feed.AddNewEntries(newEntries);
            reader.Close();

            GC.Collect();
        }
    }
}
